
//圖片上傳
function fileImgUpload(fileButtonId,targerFn) {
		$("#"+fileButtonId).uploadify({
    		height        : 20,
    		width         : 100,
   			fileSizeLimit : 1024,
    		buttonText :"上傳檔案",
    		swf           : "/"+config.projectName+'/library/uploadify/uploadify.swf',
    		uploader      : "/"+config.projectName+'/library/uploadify/uploadify.php',
   		 	fileTypeDesc : 'Image Files',
	        fileTypeExts : '*.gif; *.jpg; *.png; *.jpeg', 
	        onUploadSuccess : targerFn
    	}); 
}

function toString(o){
    var parse = function(_o){
        var a = [], t;
        for(var p in _o){
            if(_o.hasOwnProperty(p)){
                t = _o[p];
                if(t && typeof t == "object"){
                    a[a.length]= "\""+p + "\":{ " + arguments.callee(t).join(", ") + "}";
                }else {
                    if (t == null) {
                    	   a[a.length] = [ "\""+p+ "\": \"" + null + "\"" ];
                    } else if(typeof t == "string"){
                    
                        a[a.length] = [ "\""+p+ "\": \"" + t.toString() + "\"" ];
                    }else{
                        a[a.length] = [ "\""+p+ "\": " + t.toString()];
                    }
                }
            }
        }
        return a;
    };
    return "{" + parse(o).join(", ") + "}";
}

function showWait(msg) {
	  $.blockUI({ 
          message: $('<h5 style="text-align:center"><img src="/'+config.projectName+'/view/images/time.gif" width="150px" /> <br/><br/>'+ (msg ? msg : "Loading....")+'</h5>'), 
          css: { 
              top:  ($(window).height() - 60) /2 + 'px', 
              left: ($(window).width() - 60) /2 + 'px', 
              width: '100px' ,
				color: '#fff',
				background: 'none',
				border:'1px',
				opacity: .7
          } 
      }); 
}

function hideWait() {
	$.unblockUI();
}

/**
 * @param {dom
 *            id} tagId
 * @param {josn
 *            array} items
 * @param {value}
 *            targetValue
 * @param {text}
 *            taggetText
 */
function addOptions(tagId, items, targetValue, taggetText) {
	var select = $("#"+tagId);
	for (var i =0; i < items.length ;i++) {
		select.append('<option value="'+items[i][targetValue]+'">'+items[i][taggetText]+'</option>');
	}
}

function setOptions(tagId , val) {
	var options = document.getElementById(tagId).options;
	for (var i =0; i < options.length ;i++) {
		 if (options[i].value == val) {
		 	options[i].selected = true;
		 	return;
		 }
	}
}

function removeOptions(tagId) {
	$('#'+tagId).find('option').remove().end();
}

function getRedioValue(tagName) {
	var dom = Ext.query("input[name="+tagName+"][type=radio]");
	if (!dom) return "";
	for (var i=0; i < dom.length;i++) {
		if (dom[i].checked) {
			return dom[i].value;
		}
	}
	return "";
}

function getOptionValue(tagId){
	return getSelectValue(document.getElementById(tagId).options);
}

function getOptionText(tagId) {
	return getSelectText(document.getElementById(tagId).options);
}

function getSelectValue(options) {
	for (var i =0 ; i< options.length; i++){
		if (options[i].selected) {
			return options[i].value;
		}
	}
}

function getSelectText(options) {
	for (var i =0 ; i< options.length; i++){
		if (options[i].selected) {
			return options[i].text;
		}
	}
}

/**
 * commit img
 * 
 * @param {string}
 *            floder name
 * @return {string} commit img path
 */
function uploadImg(floder, h ,w){
	var result  = null;// window.showModalDialog('/ad/upload/UploadForm.php?floder='+
						// floder
						// ,null,'dialogHeight:'+h+'pt;dialogWidth:'+w+'pt;center:yes;status=no,menubar=no');
	result = window.showModalDialog(config.projectName+'/upload/UploadForm.php?floder='+ floder,null,'dialogHeight:'+h+'pt;dialogWidth:'+w+'pt;center:yes;status=no,menubar=no,location=no');
	// window.open ('/ad/upload/UploadForm.php?floder='+ floder , "newWidnw",
	// "height="+h+", width="+ w +",alwaysRaise=1, z-look=1,top=0,
	// left=0,toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no,
	// status=no")//写成一行
	 
	if (result ==  null) {
		return null;
	} else {
		return "/ad/upload/"+result;
	} 
}
 
function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
    this.toArray = function () {
    	return this.items;
    };
    this.getSize = function () {
    	return this.length;
    };
	this.remove = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	};

	this.get = function(in_key) {
		return this.items[in_key];
	};

	this.set = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	};

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	};

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	};
}

function openWindow(url , width, height, property) {
    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
   
    if (property == null) {
        property = 'toolbar=no,location=no,status=no,menubar=yes,scrollbars=no,scrollbars=yes,resizable=yes, width=' + width + ', height='+height +',left='+left+',top='+top+',screenX='+left+',screenY='+top;		
	}
	
	var obj = window.open(url ,"openWin", property);
	return obj;
}

function getBrowser() {
	 var sAgent = navigator.userAgent.toLowerCase();
	  var obj = {};
		 obj.isIE = (sAgent.indexOf("msie")!=-1); // IE6.0-7
		 obj.isFF = (sAgent.indexOf("firefox")!=-1);// firefox
		 obj.isSa = (sAgent.indexOf("safari")!=-1);// safari
		 obj.isOp = (sAgent.indexOf("opera")!=-1);// opera
		 obj.isNN = (sAgent.indexOf("netscape")!=-1);// netscape
		 obj.isMa = this.isIE;// marthon
		 obj.isOther = (!sAgent.isIE && !sAgent.isFF && !sAgent.isSa && !sAgent.isOp && !sAgent.isNN && !sAgent.isSa);// unknown
																														// Browser
		 return obj;
}
